package br.urca.impl;

import java.util.List;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.transaction.Transactional;

import br.urca.dao.BiMateriaisDAO;
import br.urca.model.BiMateriais;

@Transactional
public class JPABiMateriaisDAO implements BiMateriaisDAO {
	
	private EntityManager manager;
	
	public JPABiMateriaisDAO() {
		this(null);
	}
	
	@Inject
	public JPABiMateriaisDAO(EntityManager manager) {
		this.manager = manager;
	}

	@Override
	public List<BiMateriais> getListPorTitulo(String titulo) {
		try {
			return this.manager
					.createQuery(
							"SELECT b FROM BiMateriais b WHERE b.titulo LIKE '%" + titulo + "%'",
							BiMateriais.class)
					.getResultList();
		} catch (Exception e) {
			return null;
		}
	}
	
	@Override
	public List<BiMateriais> getListPorTitulo(String titulo,
			Character tipoMaterial) {
		try {
			return this.manager
					.createQuery(
							"SELECT b FROM BiMateriais b WHERE b.titulo LIKE '%" + titulo + "%' "
									+ " AND b.biMateriaisPK.codTipoMaterial = :tipo",
							BiMateriais.class).setParameter("tipo", tipoMaterial)
					.getResultList();
		} catch (Exception e) {
			return null;
		}
	}

	@Override
	public List<BiMateriais> getListPorArea(String area) {
		try {
			return this.manager
					.createQuery(
							"SELECT b FROM BiMateriais b WHERE b.area LIKE '%" + area + "%'",
							BiMateriais.class)
					.getResultList();
		} catch (Exception e) {
			return null;
		}
	}

	@Override
	public List<BiMateriais> getListPorArea(String area, Character tipoMaterial) {
		try {
			return this.manager
					.createQuery(
							"SELECT b FROM BiMateriais b WHERE b.area LIKE '%" + area + "%' "
									+ " AND b.biMateriaisPK.codTipoMaterial = :tipo",
							BiMateriais.class).setParameter("tipo", tipoMaterial)
					.getResultList();
		} catch (Exception e) {
			return null;
		}
	}

	@Override
	public BiMateriais getPorTipoNumeroMaterial(Character codTipoMaterial,
			int codNumeroMaterial) {
		try {
			return this.manager
					.createQuery(
							"SELECT b FROM BiMateriais b WHERE "
									+ " b.biMateriaisPK.codNumeroMaterial = :cod "
									+ " AND b.biMateriaisPK.codTipoMaterial = :tipo",
							BiMateriais.class)
					.setParameter("tipo", codTipoMaterial)
					.setParameter("cod", codNumeroMaterial).getSingleResult();
		} catch (Exception e) {
			return null;
		}
	}

	@Override
	public List<BiMateriais> getListPorISBNISSN(String filtro) {
		try {
			return this.manager
					.createQuery(
							"SELECT b FROM BiMateriais b WHERE "
									+ " b.isbn = :filtro OR (b.issn = :filtro)",
							BiMateriais.class).setParameter("filtro", filtro)
					.getResultList();
		} catch (Exception e) {
			return null;
		}
	}

	@Override
	public BiMateriais getPorSerie(Short codSerie) {
		try {
			return this.manager
					.createQuery(
							"SELECT b FROM BiMateriais b WHERE "
									+ " b.codSerie = :codigo",
							BiMateriais.class).setParameter("codigo", codSerie)
					.getSingleResult();
		} catch (Exception e) {
			return null;
		}
	}

	@Override
	public BiMateriais getPorSerie(Short codSerie,
			Character tipoMaterial) {
		try {
			return this.manager
					.createQuery(
							"SELECT b FROM BiMateriais b WHERE "
									+ " b.codSerie = :codigo "
									+ " AND b.biMateriaisPK.codTipoMaterial = :tipo",
							BiMateriais.class).setParameter("codigo", codSerie)
					.setParameter("tipo", tipoMaterial).getSingleResult();
		} catch (Exception e) {
			return null;
		}
	}
	
	@Override
	public List<BiMateriais> getPorEditora(Integer codEditora) {
		try {
			return this.manager
					.createQuery(
							"SELECT b FROM BiMateriais b WHERE "
									+ " b.codEditora = :codigo",
							BiMateriais.class).setParameter("codigo", codEditora)
					.getResultList();
		} catch (Exception e) {
			return null;
		}
	}

	@Override
	public List<BiMateriais> getPorEditora(Integer codEditora,
			Character tipoMaterial) {
		try {
			return this.manager
					.createQuery(
							"SELECT b FROM BiMateriais b WHERE "
									+ " b.codEditora = :codigo "
									+ " AND b.biMateriaisPK.codTipoMaterial = :tipo",
							BiMateriais.class).setParameter("codigo", codEditora)
					.setParameter("tipo", tipoMaterial).getResultList();
		} catch (Exception e) {
			return null;
		}
	}

	

}
